package cn.snailhd.core.base.domain;

import java.util.Date;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.snailhd.core.base.constant.MessageEnums;
import cn.snailhd.core.base.constant.ResponseConstant;

/**
 * 统一响应数据类型
 * Title: BaseResultDTO.java
 * Description:
 * Copyright by www.rsrtech.net
 * @author huangdian 
 * @date 2018年6月11日 
 * @version 1.0
 */
public class BaseResultDTO {
	
	private String code;// 通用code
	private String errorCode; //错误详情 code
	private String msg; //
	private JSONObject result = new JSONObject();
	
	
	public BaseResultDTO setSuccess(){
		this.errorCode = null;
		this.code = ResponseConstant.SUCCESS;
		this.msg = ResponseConstant.SUCCESS_MSG;
		return this;
	}
	
	public BaseResultDTO setError(){
		setError(ResponseConstant.ERROR_MSG);
		return this;
	}
	public BaseResultDTO setError(String msg){
		setErrorCode(ResponseConstant.ERROR, msg);
		return this;
	}
	
	public BaseResultDTO setErrorCode(String errorCode,String msg){
		this.code = ResponseConstant.ERROR;
		this.errorCode = errorCode;
		this.msg = msg;
		return this;
	}
	
	public BaseResultDTO setErrorCode(MessageEnums message){
		return setErrorCode(message.getErrorCode(),message.getMsg());
	}
	
	
	
	public String toJSON(){
		return JSON.toJSONString(this);
	}
	public JSONObject put(String key,Object value){
		result.put(key, value);
		return result;
	}
	
	public Object get(String key){
		return result.get(key);
	}
	public String getString(String key){
		return result.getString(key);
	}
	public Integer getInteger(String key){
		return result.getInteger(key);
	}
	public Long getLong(String key){
		return result.getLong(key);
	}
	public Float getFloat(String key){
		return result.getFloat(key);
	}
	public Double getDouble(String key){
		return result.getDouble(key);
	}
	public Date getDate(String key){
		return result.getDate(key);
	}
	public JSONObject getJSONObject(String key){
		return result.getJSONObject(key);
	}
	public JSONArray getJSONArray(String key){
		return result.getJSONArray(key);
	}
	public <T> T  getObject(String key, Class<T> clazz){
		return result.getObject(key, clazz);
	}

	
	
	
	public String getCode() {
		return code;
	}
	public BaseResultDTO setCode(String code){
		this.code = code;
		return this;
	}
	public String getErrorCode() {
		return errorCode;
	}
	public void setErrorCode(String errorCode){
		this.code = ResponseConstant.ERROR;
		this.errorCode = errorCode;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	
	public JSONObject getResult() {
		return result;
	}
	
	
	/**
	 * 替换result
	 * @param result
	 * @return
	 */
	public BaseResultDTO setResult(JSONObject result) {
		this.result = result;
		return this;
	}
	
	/**
	 * 替换result
	 * @param obj
	 * @return
	 */
	public BaseResultDTO setResult(Object obj){
		JSONObject json = JSONObject.parseObject(JSON.toJSONString(obj));
		return setResult(json,true);
	}
	
	/**
	 * 
	 * @param obj
	 * @param cover 为true 时 清除并覆盖原reslt,false 时 合并替换
	 * @return
	 */
	public BaseResultDTO setResult(Object obj,boolean cover){
		JSONObject json = JSONObject.parseObject(JSON.toJSONString(obj));
		return setResult(json,cover);
	}
	
	/**
	 * 
	 * @param json
	 * @param cover 为true 时 清除并覆盖原reslt,false 时 合并替换
	 * @return
	 */
	public BaseResultDTO setResult(JSONObject json,boolean cover){
		if(cover)
			this.result = json;
		else
			this.result.putAll(json);
		
		return this;
	} 
	
}
